package cn.glutnn.service.impl;

import cn.glutnn.entity.Cart;
import cn.glutnn.entity.Food;
import cn.glutnn.entity.OrderDetail;
import cn.glutnn.mapper.OrdersMapper;
import cn.glutnn.service.CartService;
import cn.glutnn.service.FoodService;
import cn.glutnn.service.OrderDetailService;
import cn.glutnn.service.OrdersService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import cn.glutnn.entity.Orders;
import cn.glutnn.entity.Business;
import cn.glutnn.mapper.BusinessMapper;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.io.Serializable;

/**
 * 订单表(Orders)表服务实现类
 *
 * @author msh
 * @since 2024-12-12 16:54:13
 */
@Service("ordersService")
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {
    @Resource
    private CartService cartService;
    @Resource
    private OrderDetailService orderDetailService;
    @Resource
    private FoodService foodService;
    @Resource
    private BusinessMapper businessMapper;

    @Override
    public Integer addOrder(Orders orders) {
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getUserId, orders.getUserId());
        wrapper.eq(Cart::getBusinessId, orders.getBusinessId());
        List<Cart> carts = cartService.list(wrapper);
        Date date = new Date(System.currentTimeMillis());
        orders.setOrderDate(date);
        save(orders);
        List<OrderDetail> orderDetails = new ArrayList<>();
        carts.forEach(cart -> {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOrderId(orders.getOrderId());
            orderDetail.setFoodId(cart.getFoodId());
            orderDetail.setQuantity(cart.getQuantity());
            orderDetails.add(orderDetail);
            // 更新库存
            foodService.updateStock(cart.getFoodId(), -cart.getQuantity());
        });
        orderDetailService.saveBatch(orderDetails);
        cartService.remove(wrapper);


        return orders.getOrderId();
    }

    @Override
    public void updateOrderStatus(String orderId, String status, Integer paymentMethodId, Integer daId) {
        Orders order = this.getById(orderId);
        if (order != null) {
            order.setOrderState("PAID".equals(status) ? 1 : 0);
            if (paymentMethodId != null) {
                order.setPaymentMethodsId(paymentMethodId);
            }
            if (daId != null) {
                order.setDaId(daId);
            }
            this.updateById(order);
        }
    }

    @Override
    public Orders getById(Serializable orderId) {
        Orders order = super.getById(orderId);
        if (order != null) {
            // 获取商家信息
            Business business = businessMapper.selectById(order.getBusinessId());
            if (business != null) {
                order.setShopName(business.getBusinessName());
            }
        }
        return order;
    }
}

